package com.service.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.edu.pojo.Course;
import com.service.edu.pojo.vo.CourseFrontVo;
import com.service.edu.pojo.vo.CourseInfoVo;
import com.service.edu.pojo.vo.CourseQuery;
import com.service.edu.service.CourseDescriptionService;
import com.service.edu.service.CourseService;
import com.service.edu.service.TeacherService;
import com.service.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-28
 */
@RestController
@RequestMapping("course")
@Api(description="课程管理")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private TeacherService teacherService;

    @GetMapping("queryCreateCount/{day}")
    @ApiOperation("查询某一个添加课程人数")
    public Integer queryRegisterCount(@PathVariable String day){
        return   courseService.queryCreateCount(day);
    }

    /**
     * 查询课程用户页面分页
     * @param courseFrontVo
     * @return
     */
    @PostMapping("pageCourse")
    @ApiOperation("查询课程分页对象")
    public ResultVo pageCourse(@ApiParam(name = "pageNum",value = "当前页数")  @RequestParam(defaultValue = "1",value = "pageNum")  Integer pageNum,
                               @ApiParam(name = "rows",value = "每页显示条数")  @RequestParam(defaultValue = "10",value = "rows") Integer rows,
                               @ApiParam(name = "courseQuery",value = "课程查询对象封装",required = true) @RequestBody CourseFrontVo courseFrontVo){
        return courseService.pageCourse(pageNum,rows,courseFrontVo);
    }

    @GetMapping("limitOrderByTime")
    @ApiOperation("查询最新前8条课程数据")
    @Cacheable(value = "guli:index:course",key = "'list'")
    public ResultVo limitOrderByTime(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("gmt_create");
        queryWrapper.last("limit 8");
        List<Course> list = courseService.list(queryWrapper);
        return ResultVo.ok().data("items",list);
    }


    /**
     * 查询课程分页对象
     * @param courseQuery
     * @return
     */
    @PostMapping("page")
    @ApiOperation("查询课程分页对象")
    public ResultVo page(@ApiParam(name = "courseQuery",value = "课程查询对象封装",required = true) @RequestBody CourseQuery courseQuery){
       return courseService.pages(courseQuery);
    }


    /**
     * 根据课程id 查询课程的详细信息
     * @param id
     * @return
     */
    @GetMapping("queryCourseVo/{id}")
    @ApiOperation("根据课程id查询课程的详细信息")
    public ResultVo queryCourseVo(@ApiParam(name = "id",value = "课程id",required = true) @PathVariable String id){
        Map<String,Object> map =  courseService.queryCourseVo(id);

        return ResultVo.ok().data(map);
    }

    /**
     * 添加课程信息 添加成功之后返回课程id
     * @param courseInfoVo
     * @return
     */
    @CacheEvict(value = "guli:index:course",key="'list'")   //方法执行后，删除缓存中的数据
    @PostMapping
    @ApiOperation("添加课程信息")
    public ResultVo save(@ApiParam(name = "courseInfoVo",value = "课程信息",required = true) @RequestBody CourseInfoVo courseInfoVo){
        String id = courseService.saveCourse(courseInfoVo);
        if (StringUtils.isEmpty(id)){
            return ResultVo.error();
        }
        return ResultVo.ok().data("id",id);
    }

    /**
     * 查询指定课程的基本信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("查询指定课程的基本信息")
    public ResultVo queryById(@ApiParam(name = "id",value = "课程id",required = true) @PathVariable String id){
        Course course = courseService.getById(id);
        if (StringUtils.isEmpty(course)){
            return ResultVo.error();
        }
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course,courseInfoVo);

        courseInfoVo.setDescription(courseDescriptionService.getById(courseInfoVo.getId()).getDescription());
        courseInfoVo.setTeacher(teacherService.getById(courseInfoVo.getTeacherId()));
        return ResultVo.ok().data("item",courseInfoVo);
    }

    /**
     * 修改课程信息及课程简介信息 修改成功之后返回课程id
     * @param courseInfoVo
     * @return
     */
    @CacheEvict(value = "guli:index:course",key="'list'")
    @PutMapping
    @ApiOperation("修改课程信息")
    public ResultVo update(@ApiParam(name = "courseInfoVo",value = "课程信息",required = true) @RequestBody CourseInfoVo courseInfoVo){
        String id = courseService.updateCourse(courseInfoVo);
        if (StringUtils.isEmpty(id)){
            return ResultVo.error();
        }
        return ResultVo.ok().data("id",id);
    }

    /**
     * 修改课程信息发布课程
     * @param course
     * @return
     */
    @CacheEvict(value = "guli:index:course",key="'list'")
    @PutMapping("publishCourse")
    @ApiOperation("修改课程信息发布课程")
    public ResultVo publishCourse(@ApiParam(name = "courseInfoVo",value = "课程信息",required = true) @RequestBody Course course){
        boolean b = courseService.updateById(course);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 逻辑删除课程信息
     * @param id
     * @return
     */
    @CacheEvict(value = "guli:index:course",key="'list'")
    @DeleteMapping("{id}")
    @ApiOperation("逻辑删除课程信息")
    public ResultVo delete(@ApiParam(name = "id",value = "逻辑删除课程id",required = true) @PathVariable String id){
        boolean b = courseService.removeById(id);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @CacheEvict(value = "guli:index:course",key="'list'")
    @GetMapping("updateIsDelete/{id}")
    @ApiOperation("恢复逻辑删除状态")
    public ResultVo updateIsDelete(@ApiParam(name = "id",value = "恢复逻辑删除状态课程id",required = true)@PathVariable String id){
        int i = courseService.updateIsDelete(id);
        if (i>0){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @PutMapping("updateViewsCount/{id}")
    @ApiOperation("课程浏览次数加1")
    public ResultVo updateViewsCount(@PathVariable String id){

        int b = courseService.updateViewsCount(id);
        if (b>0){
            return  ResultVo.ok();
        }
        return ResultVo.error();
    }





}

